package com.hsmw.api.service;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.config.jwt.LoginUser;
import com.hsmw.api.mapper.HsmwReportInfoMapper;
import com.hsmw.api.mapper.HsmwUserDriverMapper;
import com.hsmw.api.mapper.HsmwVehicleRefuelingMapper;
import com.hsmw.common.base.BaseService;
import com.htn.common.core.constant.PicTypeEnum;
import com.hsmw.api.utils.CommonUtils;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwVehicleRefuelingService extends BaseService<HsmwVehicleRefuelingMapper, HsmwVehicleRefueling> {


    @Autowired
    private HsmwVehicleRefuelingMapper hsmwVehicleRefuelingMapper;

    @Autowired
    private HsmwUserDriverMapper hsmwUserDriverMapper;

    @Autowired
    private CurrentHsmwUserUtils currentHsmwUserUtils;

    @Autowired
    private HsmwReportInfoMapper hsmwReportInfoMapper;

    @Autowired
    private HsmwPictureService hsmwPictureService;

    @Autowired
    private HsmwVehicleService hsmwVehicleService;

    /**
     *
     * @param hsmwVehicleRefueling
     * @return
     */
    public int vehicleRefuelingSave(HsmwVehicleRefueling hsmwVehicleRefueling) {
        LoginUser currentHsmwUser = currentHsmwUserUtils.getCurrentUserInfo();
        if(StringUtils.isNotBlank(hsmwVehicleRefueling.getDriverId())){
            HsmwUserDriver hsmwUserDriver = hsmwUserDriverMapper.selectById(hsmwVehicleRefueling.getDriverId());
            if(hsmwUserDriver != null){
                hsmwVehicleRefueling.setVehicleId(hsmwUserDriver.getVehicleId());
                HsmwVehicle vehicle = hsmwVehicleService.getById(hsmwUserDriver.getVehicleId());
                if(StringUtils.isNotNull(vehicle) && StringUtils.isNotBlank(vehicle.getVehiclePlate())){
                    hsmwVehicleRefueling.setVehiclePlate(vehicle.getVehiclePlate());
                }
            }
        }
        String reportId = CommonUtils.generateId(hsmwVehicleRefueling);
        HsmwReportInfo reportInfo = new HsmwReportInfo();
        reportInfo.setReportId(reportId);
        reportInfo.setReportType("9");
        reportInfo.setName(currentHsmwUser.getName());
        reportInfo.setUserId(currentHsmwUser.getId());
        reportInfo.setReadFlag(false);
        hsmwReportInfoMapper.insert(reportInfo);

        int insert = hsmwVehicleRefuelingMapper.insert(hsmwVehicleRefueling);
        if (CollectionUtil.isNotEmpty(hsmwVehicleRefueling.getPicList())) {
            List<HsmwPicture> pictures = new ArrayList<>();
            List<HashMap> picList = hsmwVehicleRefueling.getPicList();
            if (null != picList && picList.size() > 0) {
                for (HashMap<String, String> pic : picList) {
                    HsmwPicture temp = new HsmwPicture();
                    temp.setPicPath(pic.get("picPath"));
                    temp.setPicRealPath(pic.get("realPath"));
                    temp.setPortId(hsmwVehicleRefueling.getId());
                    temp.setCreateTime(LocalDateTime.now());
                    temp.setPicType(PicTypeEnum.DRIVER_REFUELING.getValue());
                    pictures.add(temp);
                }
            }
            hsmwPictureService.saveBatch(pictures);
        }

        return insert;

    }


    public List<HsmwVehicleRefueling> findVehicleRefuelingInfoByDriId(String id) {

        List<HsmwVehicleRefueling> hsmwVehicleRefuelings = hsmwVehicleRefuelingMapper.selectList(new LambdaQueryWrapper<HsmwVehicleRefueling>().eq(HsmwVehicleRefueling::getDriverId, id).orderByDesc(HsmwVehicleRefueling::getCreateTime));

        for (HsmwVehicleRefueling hsmwVehicleRefueling : hsmwVehicleRefuelings) {

            List<HsmwPicture> list = hsmwPictureService.list(new LambdaQueryWrapper<HsmwPicture>().eq(HsmwPicture::getPortId, hsmwVehicleRefueling.getId()));
            if (CollectionUtil.isNotEmpty(list)) {
                hsmwVehicleRefueling.setHsmwPictures(list);
            }
        }
        return hsmwVehicleRefuelings;
    }

    /**
     * 分页查询 司机加油列表信息
     * @param page
     * @param hsmwVehicleRefueling
     * @return
     */
    public IPage<HsmwVehicleRefueling> getHsmwVehicleRefuelingList(IPage page, HsmwVehicleRefueling hsmwVehicleRefueling) {

        IPage<HsmwVehicleRefueling> orderIPage = hsmwVehicleRefuelingMapper.getHsmwVehicleRefuelingList(page,hsmwVehicleRefueling);

        List<HsmwVehicleRefueling> records = orderIPage.getRecords();
        for (HsmwVehicleRefueling record : records) {
            List<HsmwPicture> list = hsmwPictureService.list(new LambdaQueryWrapper<HsmwPicture>().eq(HsmwPicture::getPortId, record.getId()));
            if (CollectionUtil.isNotEmpty(list)) {
                record.setHsmwPictures(list);
            }
        }
        return orderIPage;
    }

    public int updateVehicleRefueling(HsmwVehicleRefueling hsmwVehicleRefueling) {

        return hsmwVehicleRefuelingMapper.updateById(hsmwVehicleRefueling);
    }
}
